package com.example.liyuehui.paging;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.arch.paging.PagedList;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.example.liyuehui.paging.adapter.PagingAdapter;
import com.example.liyuehui.paging.datasource.TextDataSourceFactory;
import com.example.liyuehui.paging.diff.TvDiff;
import com.example.liyuehui.paging.presenter.DemoPresenter;
import com.example.liyuehui.paging.viewmodel.TextViewModel;


public class MainActivity extends AppCompatActivity {
	int i = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		RecyclerView recyclerView = findViewById(R.id.recycler);
		LinearLayoutManager l = new LinearLayoutManager(this);
		recyclerView.setLayoutManager(l);
		final PagingAdapter adapter = new PagingAdapter(new TvDiff());
		recyclerView.setAdapter(adapter);
		TextViewModel textViewModel = ViewModelProviders.of(this).get(TextViewModel.class);
		LiveData<PagedList<String>> liveData = textViewModel.getPagedListLiveData();

		liveData.observe(this, new Observer<PagedList<String>>() {
			@Override
			public void onChanged(@Nullable PagedList<String> strings) {
				Log.e("-", "submitList:" + strings.size());
				adapter.submitList(strings);

			}
		});

		LiveData<TextDataSourceFactory.NetStatus> netStatusLiveData = textViewModel.getNetStatusLiveData();
		netStatusLiveData.observe(this, new Observer<TextDataSourceFactory.NetStatus>() {
			@Override
			public void onChanged(@Nullable TextDataSourceFactory.NetStatus netStatus) {
				Log.e("-", "onChange:" + netStatus);
				adapter.setNetStatus(netStatus);
			}
		});
		DemoPresenter present = new DemoPresenter();
		getLifecycle().addObserver(present);
	}


}
